blog

Home / DeveloperSection / Blogs / Create and Run a html page in nodejs in Windows OS

Create and Run a html page in nodejs in Windows OS

Create and Run a html page in nodejs in Windows OS

Anonymous User 5125 25-Jan-2014

n this blog, I’m explaining how to create an html page in nodejs and run in browser.

1.   Let's start with the server module. Create the file server.js in the root directory of your project, and fill it with the following code:

var http = require('http'),
    fs = require('fs');
    fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err;
    }
    http.createServer(function (request, response) {
        response.writeHeader(200, { "Content-Type": "text/html" });
        response.write(html);
        response.end();
    }).listen(8888);
});

2.  Create a html page “index.html” write code



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <center><h3>Hello World</h3></center>
</body>
</html>

1.       Open command prompt and go to your file location.

2.       Execute your server.js file like thisCreate and Run a html page in nodejs in Windows OS

Now start your server…

1.    Now, open your browser and point it at http://localhost:8888/. This should display a web page that says "Hello World".

   Output 

 Create and Run a html page in nodejs in Windows OS


in my next post i'll explain about Angular JS


Updated 03-Feb-2020
I am a content writter !

Leave Comment

Comments

Liked By